package everydayGrade.t3;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @Author: Siantar
 * @Date: 2023-09-11-8:10
 * @Description: 1.0
 */
public class T0630 {
    public int scheduleCourse(int[][] course) {
        Arrays.sort(course, (a, b) -> a[1] - b[1]);
        PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
        int sum=0;
        for(int []c:course){
            int ti=c[0],di=c[1];
            if (ti+sum<=di){
                sum+=di;
                pq.offer(ti);
            }else if(!pq.isEmpty()&&pq.peek()>ti){
                sum-=pq.poll()-ti;
                pq.offer(ti);
            }
        }
        return pq.size();
    }
}
